package org.vaadinpa.logic.commands;

import org.vaadinpa.data.NewsItem;
import org.vaadinpa.data.Planet;
import org.vaadinpa.data.PlanetaryScan;
import org.vaadinpa.logic.Command;

public class PlanetaryScanCommand extends Command {
	public final Planet target;

	public PlanetaryScanCommand(Planet from, Planet to) {
		super(from);

		target = to;

		from.availablePlanetScans--;
	}

	@Override
	public void execute() {

		PlanetaryScan scan = new PlanetaryScan(target, planet.level);
		planet.scans.put(target.coords, scan);

		NewsItem i = new NewsItem("Planetary scan completed", "Planetary scan of " + planet.toString()
				+ " was completed. Results:<br/>" + scan.getNewsReportHTML());
		planet.addNews(i);
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void cancel() {
		planet.availablePlanetScans++;
	}

	@Override
	public String getDescription() {
		return "Target: " + target.name + " (" + target.coords + ")";
	}

	@Override
	public String getName() {
		return "Planetary Scan";
	}

	@Override
	public void serialize(StringBuilder sb) {
		super.serializeBase(sb);
		sb.append("<target x=\"" + target.coords.x + "\" y=\"" + target.coords.y + "\"/>");

		sb.append("</" + this.getClass().getSimpleName() + ">");
	}
}
